home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1996 February: Tool Chest / Apple Developer CD Series Tool Chest February 1996 (Apple Computer)(1996).iso / Sample Code / AOCE Sample Code / PowerTalk Access Modules / Sample SMSAM / SampleSMSAM Source / BuildingBlocks / DebugStream.cp < prev    next >
Encoding:
Text File  |  1995-07-28  |  6.7 KB  |  320 lines  |  [TEXT/MPS ]

  1. /*
  2.     File:        DebugStream.cp
  3.  
  4.     Copyright:    © 1991-1994 by Apple Computer, Inc.
  5.                 All rights reserved.
  6.  
  7.     Part of the AOCE Sample SMSAM Package.  Consult the license
  8.     which came with this software for your specific legal rights.
  9.  
  10. */
  11.  
  12.  
  13.  
  14. #pragma trace off
  15.  
  16. #ifndef __BOVINESERVER__
  17. #include "BovineServer.h"
  18. #endif
  19.  
  20. #ifndef __DEBUGSTREAM__
  21. #include "DebugStream.h"
  22. #endif
  23.  
  24. #ifndef __EVENTS__
  25. #include "Events.h"
  26. #endif
  27.  
  28. #ifndef    __IOSTREAM__
  29. #include <IOStream.h>
  30. #endif
  31.  
  32. #ifndef __MEMORY__
  33. #include <Memory.h>
  34. #endif
  35.  
  36. #ifndef __OSEVENTS__
  37. #include <OSEvents.h>
  38. #endif
  39.  
  40. #ifndef __PACKAGES__
  41. #include <Packages.h>
  42. #endif
  43.  
  44. #ifndef __TRANSCRIPTWINDOW__
  45. #include "TranscriptWindow.h"
  46. #endif
  47.  
  48. #ifndef __UTILITIES__
  49. #include "Utilities.h"
  50. #endif
  51.  
  52. /***********************************|****************************************/
  53.  
  54. #pragma segment DebugStream
  55.  
  56. /***********************************|****************************************/
  57.  
  58. extern Boolean gBovineServerWasInitialized;
  59.  
  60.  
  61. #define    newWindow    1
  62.  
  63. TTranscriptWindow*        gTranscriptWindow = nil;
  64.  
  65.  
  66.  
  67. /***********************************|****************************************/
  68.  
  69. debugstream::debugstream (streambuf *buf) :
  70.     ostream(buf),
  71.     ios ( buf ) 
  72. {
  73.     fEncodedCharacterOutput = false;
  74. }
  75.  
  76. /***********************************|****************************************/
  77.  
  78. debugstream::~debugstream () 
  79. {
  80. }
  81.  
  82. /***********************************|****************************************/
  83.  
  84. void debugstream::SetEncodedOutput (Boolean newValue)
  85. {
  86.     fEncodedCharacterOutput = newValue;
  87. }
  88.  
  89. /***********************************|****************************************/
  90.  
  91. debugstream& debugstream::write(register const char*  s, register int n) 
  92. {
  93.     if (fEncodedCharacterOutput) 
  94.     {
  95.         for (register int i = 0; i < n; ++i) 
  96.         {
  97.             register unsigned char c = *(s + i);
  98.  
  99.             if (c < 0x80) 
  100.             {
  101.                 switch (c) 
  102.                 {
  103.                     case 0:    ostream::operator << ( "^@" ); break;
  104.                     case 9: ostream::operator << ( "<tab>" ); break;
  105.                     case 10: ostream::operator << ( "<lf>" ); break;
  106.                     case 13: ostream::operator << ( "<cr>" ); break;
  107.                     case 27: ostream::operator << ( "<esc>" ); break;
  108.                     default: *this << c; break;
  109.                 }
  110.             }
  111.             else 
  112.             {
  113.                 *this << "<" << hexo << (int) c << deco << ">";
  114.             }
  115.         }
  116.  
  117.         fEncodedCharacterOutput = false;
  118.     } 
  119.     else 
  120.     {
  121.         ostream::write ( s, n );
  122.     }
  123.     
  124.     return *this;
  125. }
  126.  
  127. /***********************************|****************************************/
  128.  
  129. TWWStreamBuf::TWWStreamBuf () 
  130. {
  131.     const    long wwstreambufSize = 80;
  132.  
  133.     //    Create a buffer to store data into.
  134.     char *buffer = (char*) ::NewPtrClear(wwstreambufSize);
  135.     setbuf (buffer, (int) wwstreambufSize);
  136.  
  137.     //    Give the entire buffer (save one character) to the put area
  138.     setp (base(), ebuf()-1);
  139. }
  140.  
  141. /***********************************|****************************************/
  142.  
  143. TWWStreamBuf::~TWWStreamBuf () 
  144. {
  145.     gBovineServerWasInitialized = false;    // this is really a “debug streaming is doable” flag
  146.  
  147.     //    Dispose of the memory we allocated to store the buffer
  148.     if ( base())
  149.         DisposePtr( (Ptr) base());
  150. }
  151.  
  152. /***********************************|****************************************/
  153.  
  154. short GetCurrentModifiers()
  155. {
  156.     EventRecord    event;
  157.     (void) OSEventAvail(0, &event);
  158.     return event.modifiers;
  159. }
  160.  
  161. /***********************************|****************************************/
  162.  
  163. inline Boolean ShiftDown(short modifiers)
  164.     { return (modifiers & shiftKey) != 0; }
  165.  
  166. /***********************************|****************************************/
  167.  
  168. inline Boolean CommandDown(short modifiers)
  169.     { return (modifiers & cmdKey) != 0; }
  170.  
  171. /***********************************|****************************************/
  172.  
  173. inline Boolean OptionDown(short modifiers)
  174.     { return (modifiers & optionKey) != 0; }
  175.  
  176. /***********************************|****************************************/
  177.  
  178. Boolean ShiftCurrentlyDown()
  179.     {
  180.     return ShiftDown(GetCurrentModifiers());
  181.     }
  182.  
  183. /***********************************|****************************************/
  184.  
  185. Boolean OptionCurrentlyDown()
  186.     {
  187.     return OptionDown(GetCurrentModifiers());
  188.     }
  189.  
  190. /***********************************|****************************************/
  191.  
  192. Boolean CommandCurrentlyDown()
  193.     {
  194.     return CommandDown(GetCurrentModifiers());
  195.     }
  196.  
  197. /***********************************|****************************************/
  198.  
  199. int TWWStreamBuf::overflow (int c) 
  200. {
  201.     //    Make sure we've got a buffer to dump stuff into.
  202.     if (allocate() == EOF)
  203.         return (EOF);
  204.     else if ( base() == nil)
  205.         return (EOF);
  206.  
  207.     //    If the user holds down both command and option, then don't show
  208.     //    any output -- just immediately return.
  209.     Boolean skipOutput = (CommandCurrentlyDown() && OptionCurrentlyDown());
  210.     if (skipOutput) {
  211.         setp (base(), ebuf()-1);
  212.         return 0;
  213.     }
  214.         
  215.     #ifdef THINK_CPLUS
  216.     //    In Think, '/n' is not carriage return.  So, change it.
  217.     if ( (c == '\r') || ( c == '\n') )
  218.         c = 13;
  219.     #endif
  220.  
  221.     //    Put the characters in the existing buffer into the debugging stream
  222.     long len = pptr() - pbase();
  223.     if (c == EOF) 
  224.     {
  225.         #if newWindow
  226.         if ( gTranscriptWindow )
  227.             gTranscriptWindow->Append ( pbase(), len );
  228.         #else
  229.         WWAddText (pbase(), len);
  230.         WWFlushOutputFile();
  231.         #endif
  232.     } else {
  233.         *pptr() = c;
  234.         
  235.         #if newWindow
  236.         if ( gTranscriptWindow )
  237.         {
  238.             gTranscriptWindow->Append ( pbase(), len + 1 );
  239.             gTranscriptWindow->FlushOutputFile ();
  240.         }
  241.         #else
  242.         WWAddText (pbase(), len + 1);
  243.         
  244.         if ( keithFlag.Flag(23))
  245.             WWFlushOutputFile();
  246.         #endif
  247.     }
  248.     
  249.     if (ShiftCurrentlyDown() && ( IsGatewayPausing() == false ))
  250.         TYield();
  251.  
  252.     //    Reset this buffer to the entire buffer space
  253.     setp (base(), ebuf()-1);
  254.  
  255.     return (0);
  256. }
  257.  
  258. /***********************************|****************************************/
  259.  
  260. int TWWStreamBuf::underflow () 
  261. {
  262.     //    We don't support input in this stream.
  263.     return (EOF);
  264. }
  265.  
  266. /***********************************|****************************************/
  267.  
  268. #if 1
  269. int TWWStreamBuf::sync ( )
  270. {
  271.     overflow( EOF );
  272.     return 0;
  273. }
  274. #endif
  275.  
  276. /***********************************|****************************************/
  277.  
  278. // put the date (short format) into the output stream
  279. ostream&    date (ostream& o) 
  280. {
  281.     Str255    theDate;
  282.     unsigned long    now;
  283.  
  284.     GetDateTime (&now);
  285.     IUDateString (now, longDate, theDate);
  286.  
  287.     o.write (&theDate[1], theDate[0]);
  288.     o.put (' ');
  289.  
  290.     return o;
  291. }
  292.  
  293. /***********************************|****************************************/
  294.  
  295. // put the time into the output stream
  296. ostream&    time (ostream& o) 
  297. {
  298.     Str255    theTime;
  299.     unsigned long    now;
  300.  
  301.     GetDateTime (&now);
  302.     IUTimeString (now, true, theTime);
  303.  
  304.     o.write (&theTime[1], theTime[0]);
  305.     o.put (' ');
  306.  
  307.     return o;
  308. }
  309.  
  310. /***********************************|****************************************/
  311.  
  312.  // makes the next item output in an encoded character format
  313. debugstream& encoded (debugstream& s) 
  314. {
  315.     s.SetEncodedOutput  (true);
  316.     return s;
  317. }
  318.  
  319. /***********************************|****************************************/
  320.